<?php

namespace app\admin\controller;

use app\common\model\Aftersales;
use app\common\model\Express as ExpressModel;
use app\common\model\GoodsRecord;
use app\common\model\Order;
use app\common\model\Products as ProductModel;
use app\common\model\ProductStock;
use app\common\model\Reserveorder;
use app\common\service\ImportData;
use app\common\service\Wechat;

class Orders extends Base
{
    public function __construct()
    {
        parent::__construct();
        $this->isLogin();
    }

    /**
     * @route('orders/shopsorder')
     * 商铺普通商品订单
     */
    public function shopsorder()
    {
        // 获取搜索条件
		$get['time'] = input('get.time');
		$get['pay_status'] = input('get.pay_status');
		$get['order_status'] = input('get.order_status');
        $get['shop_name'] = input('get.shop_name');
		$isdisplay = true;
		//判断是总部还是商家
		if($this->_shopid != 0){//有shop_id的是商家
			$get['shop_id'] = $this->_shopid;
			$isdisplay = false;
		}

		$ordersModel = new Order();

		$get['order_type'] = 1;
        $rs_data = $ordersModel->shops_order_list($get,'o.order_id desc',$this->limit,true);

        $tvar = array(
            "title" => "商铺订单列表",
            "time"  => time(),
            "data"  => $rs_data['data'],
            "get"   => $get,
			"isdisplay" => $isdisplay,
			"statelist" => $ordersModel::$statelist,
			"paystatelist" => $ordersModel::$paystatelist,
        );

        $this->assign("data", $tvar);
        $this->assign('page', $rs_data['page']);
        return $this->fetch('orderslist/shopslist');
    }

    /**
     * @route('orders/exchangeorder')
     * 兑换商品订单
     */
    public function exchangeorder()
    {
        // 获取搜索条件
		$get['time'] = input('get.time');
		$get['pay_status'] = input('get.pay_status');
		$get['order_status'] = input('get.order_status');
		$get['shop_name'] = input('get.shop_name');
		$isdisplay = true;
		//判断是总部还是商家
		if($this->_shopid != 0){//有shop_id的是商家
			$get['shop_id'] = $this->_shopid;
			$isdisplay = false;
		}

		$ordersModel = new Order();

		$get['order_type'] = 2;
        $rs_data = $ordersModel->shops_order_list($get,'o.order_id desc',$this->limit,true);

        $tvar = array(
            "title" => "商铺订单列表",
            "time"  => time(),
            "data"  => $rs_data['data'],
            "get"   => $get,
			"isdisplay" => $isdisplay,
			"statelist" => $ordersModel::$statelist,
			"paystatelist" => $ordersModel::$paystatelist,
        );

        $this->assign("data", $tvar);
        $this->assign('page', $rs_data['page']);
        return $this->fetch('orderslist/exchangelist');
    }

    /**
     * @route('orders/loveorder')
     * 爱心餐商品订单
     */
    public function loveorder()
    {
        // 获取搜索条件
		$get['time'] = input('get.time');
		$get['pay_status'] = input('get.pay_status');
		$get['order_status'] = input('get.order_status');
		$get['shop_name'] = input('get.shop_name');
		$get['is_use'] = input('get.is_use');
		$get['access_code'] = input('get.access_code');
		$isdisplay = true;
		//判断是总部还是商家
		if($this->_shopid != 0){//有shop_id的是商家
			$get['shop_id'] = $this->_shopid;
			$isdisplay = false;
		}

		$ordersModel = new Order();

		$get['order_type'] = 3;
		$item = [
		    'o.*',
            's.shop_name',
            'u.user_id',
            'u.nick_name',
            'gr.status as gr_status',
            'gr.access_code',
            'gr.get_time as gr_g_t',
        ];
        $rs_data = $ordersModel->loveandteam_order_list($get,'o.order_id desc',$this->limit,true,$item);

        $tvar = array(
            "title" => "商铺订单列表",
            "time"  => time(),
            "data"  => $rs_data['data'],
            "get"   => $get,
			"isdisplay" => $isdisplay,
			"receivelist" => $ordersModel::$receivelist,
        );

        $this->assign("data", $tvar);
        $this->assign('page', $rs_data['page']);
        return $this->fetch('orderslist/lovelist');
    }

    /**
     * @route('orders/teamorder')
     * 团餐商品订单
     */
    public function teamorder()
    {
        // 获取搜索条件
		$get['time'] = input('get.time');
		$get['pay_status'] = input('get.pay_status');
		$get['order_status'] = input('get.order_status');
		$get['shop_name'] = input('get.shop_name');
		$get['is_use'] = input('get.is_use');
        $get['access_code'] = input('get.access_code');
		$isdisplay = true;
		//判断是总部还是商家
		if($this->_shopid != 0){//有shop_id的是商家
			$get['shop_id'] = $this->_shopid;
			$isdisplay = false;
		}

		$ordersModel = new Order();

		$get['order_type'] = 4;
		$item = [
		    'o.*',
            's.shop_name',
            'u.user_id',
            'u.nick_name',
            'gr.status as gr_status',
            'gr.access_code',
            'gr.get_time as gr_g_t',
        ];
        $rs_data = $ordersModel->loveandteam_order_list($get,'o.order_id desc',$this->limit,true,$item);
        $tvar = array(
            "title" => "团餐订单列表",
            "time"  => time(),
            "data"  => $rs_data['data'],
            "get"   => $get,
			"isdisplay" => $isdisplay,
			"receivelist" => $ordersModel::$receivelist,
            "paystatelist" => $ordersModel::$paystatelist,
            "sexlist" => $ordersModel::$sexlist,
        );

        $this->assign("data", $tvar);
        $this->assign('page', $rs_data['page']);
        return $this->fetch('orderslist/teamlist');
    }

    /**
     * @route('orders/reserveorder')
     * 商铺预定订单
     */
    public function reserveorder()
    {
        // 获取搜索条件
        $get['time'] = input('get.time');
        $get['pay_status'] = input('get.pay_status');
        $get['status'] = input('get.order_status');
        $get['shop_name'] = input('get.shop_name');
        $isdisplay = true;
        //判断是总部还是商家
        if($this->_shopid != 0){//有shop_id的是商家
            $get['shop_id'] = $this->_shopid;
            $isdisplay = false;
        }

        $ordersModel = new Reserveorder();

        $rs_data = $ordersModel->GetReserveOrderList($get,'*',$this->limit);

        $tvar = array(
            "title" => "商铺预定订单列表",
            "time"  => time(),
            "data"  => $rs_data,
            "get"   => $get,
            "isdisplay" => $isdisplay,
            "statelist" => $ordersModel::$statelist,
            "paystatelist" => $ordersModel::$paystatelist,
            "sexlist" => $ordersModel::$sexlist,
        );

        $this->assign("data", $tvar);
        $this->assign('page', $rs_data->render());
        return $this->fetch('orderslist/reservelist');
    }
	/**
	 * @route('orders/shiping')
	 * 订单发货
	 */
	public function shiping()
	{
		$get['order_id'] = input('get.order_id');
		$get['order_status'] = 2;
		$ordersModel = new Order();


		if(input('post.submit')){
		    $express_id = input('express_id');
		    if(empty($express_id)){
		        $this->error('请选择快递公司');
            }
			$res = $ordersModel->save([
				'order_status' => 3,
				'express_id'    => $express_id,
				'tracking_number' => input('post.tracking_number'),
				'shiping_time' => time(),
			],$get);
			if(!$res){
				$this->error($ordersModel->getError());
			}
			$this->success('操作成功');
		}

		$data = $ordersModel->getOne($get);
		if(!$data){
			$this->error('操作失败');
		}
		$data = $data->toArray();
		if($data['order_type'] != 1 && $data['order_type'] != 2 ){
            $this->error('该类型不允许发货');
        }

		$ExpressModel = new ExpressModel();
		$express_data = $ExpressModel->get_list(['status' => 1],'express_id asc','express_id,express_name');

		$tvar = array(
			"title" => "商铺订单列表",
			"time"  => time(),
			'order_id' => $get['order_id'],
			'express'   => $express_data
		);

		$this->assign("data", $tvar);
		return $this->fetch('orderslist/boxshiping');
	}

    /**
     * @route('orders/boxshopdetail')
     * 商铺订单详情
     */
    public function boxshoptdetail()
    {
        $order_id = input('get.order_id');
        $orderModel = new Order();
        $order = $orderModel->getOne(['order_id'=>$order_id]);
        if(empty($order)){
            $this->error('不存在的订单详情');
        }
        $rs_data = $orderModel->shops_order_details($order_id);
//        var_dump($rs_data);die;
        // 后台账号数据
        $tvar = array(
            "title" => "商铺订单详情",
            "time"  => time(),
            "data"  => $rs_data,
            "order" => $order->toArray(),
        );
        $this->assign("data", $tvar);
        return $this->fetch('orderslist/boxshops');
    }

    /**
     * @route('orders/sureget')
     * 爱心餐团餐确认领取使用
     */
    public function sureget()
    {
        //判断是总部还是商家
        if($this->_shopid != 0){//有shop_id的是商家
            $shop_id = $this->_shopid;
        }
        $order_id = input('order_id');
		$order_type = [3,4];
        $ordersModel = new Order();

        if(input('get.handle') == 'submit'){
            $recordModel = new GoodsRecord();
            $code = input('access_number');
            $where = [
                ['o.pay_status','=',2],
                ['o.order_status','=',4],
                ['o.order_type','in',$order_type],
            ];
            isset($shop_id)?$where[] = ['o.shop_id','=',$shop_id]:'';
            isset($code)?$where[] = ['gr.access_code','=',$code]:'';
            isset($order_id)?$where[] = ['o.order_id','=',$order_id]:'';
            if(!isset($order_id) && !isset($code)){
                $this->error('类型有误');
            }
//            echo "<pre>";
//            print_r($where);
            $record = $ordersModel->getJoinOrder($where,'gr.*',[['ddt_goods_record gr','o.order_id = gr.order_id']]);
//            var_dump($record);die;
            if(empty($record)){
                $this->error('查询不到订单');
            }
            $record = $record->toArray();
            if($record['status'] == 2){
                $this->error('该订单已领取');
            }
            $res = $recordModel->EditRecord(['status'=>2,'get_time'=>time()],$record['g_r_id']);
            if(!$res){
                $this->error($recordModel->getError());
            }
            $this->success('操作成功');
        }

        $tvar = array(
            "title" => "输入确认领取界面",
            "time"  => time(),
        );

        $this->assign("data", $tvar);
        return $this->fetch('orderslist/boxshiping');
    }


    /**
     * @route('aftersales/aftersaleslist')
     * 商品售后管理列表
     */
    public function aftersaleslist()
    {
        $isdisplay = true;
        $get['shop_name'] = input('get.shop_name');
        $get['a_s_type'] = input('get.a_s_type');
        $get['is_show'] = 1;
        //判断是总部还是商家
        if($this->_shopid != 0){//有shop_id的是商家
            $get['shop_id'] = $this->_shopid;
            $isdisplay = false;
        }

        $afterModel = new Aftersales();
        $item = 'as.*,as.add_time as as_time,u.nick_name,u.avatar,u.phone,s.shop_name,s.imagesurl';
        $join = [
            ['ddt_user u','as.user_id = u.user_id'],
            ['ddt_shops s','as.shop_id = s.shop_id'],
        ];
        $rs_data = $afterModel->get_aftersales_list($get,$item,'',$this->limit,$join,true);
//        var_dump($rs_data->toArray());die;
        $tvar = array(
            "title" => "申请售后管理",
            "time"  => time(),
            "data"  => $rs_data,
            "isdisplay" => $isdisplay,
            "get" => $get,
            "afterstatus" => $afterModel::$afterstatus,
            "aftertype" => $afterModel::$aftertype,
            "refundstatus" => $afterModel::$refundstatus,
        );

        $this->assign("data", $tvar);
        $this->assign('page', $rs_data->render());
        return $this->fetch('aftersales/aftersaleslist');
    }


    /**
     * @route('aftersales/sureafter')
     * 确认售后完成
     */
    public function sureafter()
    {
        //判断是总部还是商家
        if($this->_shopid != 0){//有shop_id的是商家
            $shop_id = $this->_shopid;
        }
        $asid = input('a_s_id');
//        var_dump($asid);die;
        $afterModel = new Aftersales();
        $where = [
            ['a_s_id','=',$asid],
        ];
        isset($shop_id)?$where[] = ['shop_id','=',$shop_id]:'';
        $after = $afterModel->getOne($where,'*');
//        var_dump($after);die;
        if(empty($after)){
            $this->error('查询不到售后信息');
        }
        $after = $after->toArray();
        if($after['a_s_status'] == 2){
            $this->error('该售后已完成');
        }
        $res = $afterModel->edit(['a_s_status'=>2],$after['a_s_id']);
        if(!$res){
            $this->error($afterModel->getError());
        }
        $this->success('操作成功');
    }

    /**
     * @route('aftersales/surerefund')
     * 确认售后退款
     */
    public function surerefund()
    {
        //判断是总部还是商家
        if($this->_shopid != 0){//有shop_id的是商家
            $shop_id = $this->_shopid;
        }
        $asid = input('a_s_id');
//        var_dump($asid);die;
        $afterModel = new Aftersales();
        $where = [
            ['a_s_id','=',$asid],
        ];
        isset($shop_id)?$where[] = ['shop_id','=',$shop_id]:'';
        $after = $afterModel->getOne($where,'*');
//        var_dump($after);die;
        if(empty($after)){
            $this->error('查询不到售后信息');
        }
        $after = $after->toArray();
        if($after['is_refund'] == 2){
            $this->error('该售后已退款');
        }
        $afterModel->startTrans();
        $res = $afterModel->edit(['is_refund'=>2],$after['a_s_id']);
        if(!$res){
            $afterModel->rollback();
            $this->error($afterModel->getError());
        }
        $orderModel = new Order();
        $o_rs = $orderModel->where('order_id','=',$after['order_id'])->update(['order_status'=>6]);
        if(!$o_rs){
            $afterModel->rollback();
            $this->error($orderModel->getError());
        }
        $afterModel->commit();
        $this->success('操作成功');
    }

    /**
     * @route('orders/offlineorder')
     * 商铺线下点单订单
     */
    public function offlineorder()
    {
        // 获取搜索条件
        $get['time'] = input('get.time');
        $get['pay_status'] = input('get.pay_status');
        $get['order_status'] = input('get.order_status');
		$get['shop_name'] = input('get.shop_name');
		$get['seat_num'] = input('get.seat_num');
        $isdisplay = true;
        //判断是总部还是商家
        if($this->_shopid != 0){//有shop_id的是商家
            $get['shop_id'] = $this->_shopid;
            $isdisplay = false;
        }

        $ordersModel = new Order();

        $get['order_type'] = 5;
        $rs_data = $ordersModel->AdminGetOfflineOrderList($get,'o.order_id desc',$this->limit);
//		print_r($rs_data);die;
        $tvar = array(
            "title" => "点餐订单列表",
            "time"  => time(),
            "data"  => $rs_data->toArray(),
            "get"   => $get,
            "isdisplay" => $isdisplay,
            "paystatelist" => $ordersModel::$paystatelist,
            "statelist" => $ordersModel::$statelist,
            "specialstatuslist" => $ordersModel::$specialstatuslist,
        );

        $this->assign("data", $tvar);
        $this->assign('page', $rs_data->render());
        return $this->fetch('orderslist/offlineorderlist');
    }

    /**
     * @route('orders/sureshipments')
     * 线下订单发货
     */
    public function sureshipments()
    {
        $orderid = input('order_id');
        $orderModel = new Order();
        $data = $orderModel->getOne(['order_id'=>$orderid]);
        if(!$data){
            $this->error('不存在的订单');
        }
        if($data['order_status'] != 2){
            $this->error('不允许发货');
        }
        $res = $orderModel->edit(['order_status'=>3],$orderid);
        if(!$res){
            $this->error($orderModel->getError());
        }
        $this->success('操作成功');
    }

    /**
     * @route('orders/shoprefund')
     * 商家后台退款
     */
    public function shoprefund()
    {
        $orderid = input('order_id');
        $orderModel = new Order();
        $data = $orderModel->getOne(['order_id'=>$orderid]);
        if(!$data){
            $this->error('不存在的订单');
        }
        if($data['order_status'] != 2){
            $this->error('不允许操作');
        }
        $refundno = 'refund_'.date('Ymd').substr(implode(NULL, array_map('ord', str_split(substr(uniqid(), 7, 13), 1))), 0, 8);
        $amount_info = [
            'refund' => (int)calculate([$data['pay_amount'],100],'*',0),
            'total' => (int)calculate([$data['pay_amount'],100],'*',0),
            'currency' => 'CNY',
        ];
        $refund_data = [
            'transaction_id' => $data['payorderno'],
            'out_refund_no' => $refundno,
            'amount' => $amount_info,
        ];
        try{
            $refund_res = Wechat::OrderRefund(json_encode($refund_data));
//            $refund_res = Wechat::doRefund($amount_info['total'],$amount_info['total'],$data['payorderno'],'wx2024052351485354',$refundno);
        }catch(\Exception $e){
            $this->error($e->getMessage());
        }
//        var_dump($refund_res);die;
        $param = [
            'order_status' => 7,
            'refundno' => $refundno,
            'wx_refund_id' => $refund_res['refund_id'],
            'refund_json' => json_encode($refund_res),
            'is_refund' => 3,
        ];
        $res = $orderModel->edit($param,$orderid);
        if(!$res){
            $this->error($orderModel->getError());
        }
        $this->success('操作成功');
    }

    /**
     * @route('orders/canceluser')
     * 取消用户撤销订单
     */
    public function canceluser()
    {
        $orderid = input('order_id');
        $orderModel = new Order();
        $data = $orderModel->getOne(['order_id'=>$orderid]);
        if(!$data){
            $this->error('不存在的订单');
        }
        if($data['order_status'] != 6){
            $this->error('错误操作');
        }
        $res = $orderModel->edit(['order_status'=>3],$orderid);
        if(!$res){
            $this->error($orderModel->getError());
        }
        $this->success('操作成功');
    }

    /**
     * @route('orders/sureUserRefund')
     * 商家确认用户退款
     */
    public function sureUserRefund()
    {
        $orderid = input('order_id');
        $orderModel = new Order();
        $data = $orderModel->getOne(['order_id'=>$orderid]);
        if(!$data){
            $this->error('不存在的订单');
        }
        if($data['order_status'] != 6){
            $this->error('不允许操作');
        }
        $refundno = 'refund_'.date('Ymd').substr(implode(NULL, array_map('ord', str_split(substr(uniqid(), 7, 13), 1))), 0, 8);
        $amount_info = [
            'refund' => (int)calculate([$data['pay_amount'],100],'*',0),
            'total' => (int)calculate([$data['pay_amount'],100],'*',0),
            'currency' => 'CNY',
        ];
        $refund_data = [
            'transaction_id' => $data['payorderno'],
            'out_refund_no' => $refundno,
            'amount' => $amount_info,
        ];
        try{
            $refund_res = Wechat::OrderRefund(json_encode($refund_data));
//            $refund_res = Wechat::doRefund($amount_info['total'],$amount_info['total'],$data['payorderno'],'wx2024052351485354',$refundno);
        }catch(\Exception $e){
            $this->error($e->getMessage());
        }
//        var_dump($refund_res);die;
        $param = [
            'order_status' => 7,
            'refundno' => $refundno,
            'wx_refund_id' => $refund_res['refund_id'],
            'refund_json' => json_encode($refund_res),
            'is_refund' => 2,
        ];
        $res = $orderModel->edit($param,$orderid);
        if(!$res){
            $this->error($orderModel->getError());
        }
        $this->success('操作成功');
    }


    /**
     * @route('orders/surespecial')
     * 线下订单修改特殊状态
     */
    public function surespecial()
    {
        //判断是总部还是商家
        if($this->_shopid != 0){//有shop_id的是商家
            $shop_id = $this->_shopid;
        }
        $orderid = input('order_id');
        $status = input('status');
//        var_dump($orderid);die;
        $orderModel = new Order();
        $where = [
            ['order_id','=',$orderid],
        ];
        isset($shop_id)?$where[] = ['shop_id','=',$shop_id]:'';
        $order = $orderModel->getOne($where,'*');
//        var_dump($order);die;
        if(empty($order)){
            $this->error('查询不到订单信息');
        }
        if($order['special_status'] == 2 || $order['special_status'] == 3 || $order['pay_status'] == 2){
            $this->error('订单状态不正确');
        }
        $editdata = [
            'special_status' => $status,
        ];
        if($status == 3){
            $editdata['order_status'] = 4;
            $editdata['pay_status'] = 2;
        }
        $res = $orderModel->edit($editdata,$order['order_id']);
        if(!$res){
            $this->error($orderModel->getError());
        }
        $this->success('操作成功');
    }

    /**
     * @route('orders/cancelorder')
     * 线下订单取消
     */
    public function cancelorder()
    {
        $orderid = input('order_id');
        $orderModel = new Order();
        $where = [
            'order_id' => $orderid
        ];
        $res = $orderModel->operatecancel($where);
        if(!$res){
            $this->error($orderModel->getError());
        }
        $this->success('操作成功');
    }

    /**
     * @route('orders/UpdateMakeMeal')
     * 修改确认出餐
     */
    public function UpdateMakeMeal(){
        if(!input('get.order_id')){
            $this->error('操作失败');
        }
        $ordersModel = new Order();

        $res = $ordersModel->save(['is_make_meal' => 1],['order_id'=>input('get.order_id')]);
        if(!$res){
            $this->error($ordersModel->getError());
        }
        $this->success('操作成功');
    }

    /**
     * @route('orders/importoffline')
     * 线下订单导出
     */
    public function importoffline()
    {

        // 获取搜索条件
        $get['time'] = input('get.time');
        $get['pay_status'] = input('get.pay_status');
        //判断是总部还是商家
        if($this->_shopid != 0){//有shop_id的是商家
            $get['shop_id'] = $this->_shopid;
        }
//        var_dump($get);die;
        $ordersModel = new Order();
        $get['order_type'] = 5;
        $rs_data = $ordersModel->AdminImportOfflineOrderList($get,'*','order_id desc',$this->limit);
//        var_dump($rs_data->toArray());die;
        if(empty($rs_data->toArray())){
            $this->error('无可导出信息','/main/index');
        }
        $string = '';
        foreach($rs_data->toArray() as $key => $value){
            $data[$key] = [
                'orderno' => $value['orderno'],
                'seat' => $value['offline_records']['seat_info']['seat_num'],
                'amount' => $value['pay_amount'],
                'date' => date('Y-m-d H:i:s',$value['order_time']),
            ];
            foreach($value['order_details'] as $v){
                $string .= $v['product_name'].'--'.$v['p_s_name']."(数量:{$v['quantity']})".PHP_EOL;
            }
//            var_dump($products);die;
            $data[$key]['product'] = $string;
            $string = '';
        }
//        var_dump($data);die;
//        $data = [
//            ['title1' => '111', 'title2' => '222'],
//            ['title1' => '111', 'title2' => '222'],
//            ['title1' => '111', 'title2' => '222']
//        ];
        $title = ['订单号', '桌号','金额','日期','商品信息'];

        $import = new ImportData();
        $import->ImportData($data,$title,date('Y-m-d',time()).'.xlsx');
    }

	/**
	 * 手动添加订单（选择菜品）
	 */
	public function selectProducts()
	{
		// 获取搜索条件
		$get['shop_name'] = input('get.shop_name');
		$get['product_name'] = input('get.product_name');
		$get['state'] = input('get.state');
		$where = null;
//        判断用户类型
		$isdisplay = true;

		$isshop = $this->_shopid;
		if($isshop != 0){
			$isdisplay = false;
		}
		if(!empty($get['state'])){
			$where[] = ['p.state','=',$get['state']];
		}
		if(!empty($get['type_id'])){
			$where[] = ['pt.type_id','=',$get['type_id']];
		}
		if(!empty($get['shop_name'])){
			$where[] = ['s.shop_name','like',"%".$get['shop_name']."%"];
		}
		if(!empty($get['product_name'])){
			$where[] = ['p.product_name','like',"%".$get['product_name']."%"];
		}
		$where[] = ['p.is_del','=',1];
		$where[] = ['p.state','=',1];
		$where[] = ['p.shop_id','=',$this->_shopid];
		$where[] = ['p.class_id','=',5];
		// 后台账号数据
		$model = new ProductModel();
		$rs_data = $model
			->alias('p')
			->leftJoin([
				['ddt_shops s','s.shop_id = p.shop_id'],
				['ddt_products_type pt','pt.type_id = p.type_id'],
			])
			->where($where)
			->field('p.*,s.shop_name,pt.type_name')
			->order('p.product_id desc')
			->paginate($this->limit);
//		print_r($rs_data);die;

		$tvar = array(
			"title" => "点餐商品列表",
			"time"  => time(),
			"data"  => $rs_data,
			"get"   => $get,
			"isdisplay" => $isdisplay,
		);
		$page = $rs_data->render();
		$this->assign("data", $tvar);
		$this->assign('page', $page);
		return $this->fetch('orderslist/selectProducts');
	}

	/**
	 * 手动添加订单（选择库存）
	 */
	public function selectProductsstock()
	{
		$get['product_id'] = input('get.product_id');
		$where['p.product_id'] = $get['product_id'];
		$where['p.is_del'] = 1;
		if($this->_shopid != 0){
			$where['p.shop_id'] = $this->_shopid;
		}

		$model = new ProductModel();
		$product = $model
			->alias('p')
			->leftJoin('ddt_shops s','p.shop_id = s.shop_id')
			->where($where)
			->field('p.*,s.shop_name')
			->find();
		if(empty($product)){
			$this->error('不存在该商品','/products/shopsproduct');
		}
		if($product->toArray()['class_id'] == 4){
			$is_show = false;
		} else {
			$is_show = true;
		}

		$stockModel = new ProductStock();
		$rs_data = $stockModel->where('product_id','=',$get['product_id'])->where('is_del','=',1)->paginate($this->limit);
		$tvar = array(
			"title" => "商品库存列表",
			"time"  => time(),
			"stock"  => $rs_data,
			"get"   => $get,
			"is_show" => $is_show,
		);
		$page = $rs_data->render();
		$this->assign("data", $tvar);
		$this->assign("product", $product->toArray());
		$this->assign('page', $page);
		return $this->fetch('orderslist/selectProductsstock');
	}

	/**
	 * 手动添加订单
	 */
	public function ManualadditionOrder(\think\Request $request)
	{
		if(request()->isAjax()){
			$param = $request->param();

			$params['p_id'] = $param['typeid'];
			$params['type'] = 'offline';
			$params['type_name'] = $param['type_name'];
			$params['sort'] = $param['sort'];
			$params['admin_id'] = $this->_userid;

//			$rs = $ProductType->edit($params, $param['id']);
//			if($rs === false){
//				$this->error('操作失败');
//			}
//			$this->success('操作成功');
		}

		$get['product_id'] = input('get.product_id');
		$get['p_s_id'] = input('get.p_s_id');

		$tvar = array(
			"title" => "点餐订单列表",
			"time"  => time(),
			"get"  => $get,
		);
		$this->assign("data", $tvar);
		return $this->fetch('orderslist/manualadditionorder');
	}
}